/**
 * edited at 2024-08-30 15:20 in nanjing by cyj.
 * 9.15需求变更个人页样式
 * */
const app = getApp();
import {
    customNavigate,
    customRedirect
} from '@/utils/route'
import {
    showModal,
    showToast
} from '@/utils/util'
import {
    fetchElderlyInfo,
    fetchLogout
} from '@/services/personal';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        menus: [],
        userInfo: {},
        safeArea: {},
        haveWatch: false
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        const that = this;
        const menus = [{
            iconUrl: `${app.globalData.STATIC_PREFIX}/static/images/icon-personal-archives.png`,
            name: '老人档案',
            id: 'elderly_archives',
            path: '/User/pages/Record/index'
        }, {
            iconUrl: app.globalData.STATIC_PREFIX + '/static/images/icon-personal-analyze.png',
            name: '健康分析',
            id: 'health_analysis',
            path: '/Health/pages/List/index'
        }, {
            iconUrl: app.globalData.STATIC_PREFIX + '/icons/icon-personal-devicelist.png',
            name: '设备列表',
            id: 'device_list',
            path: '/Device/pages/List/index'
        }, {
            iconUrl: app.globalData.STATIC_PREFIX + '/static/images/icon-personal-setting.png',
            name: '系统设置',
            id: 'system_setting',
            path: '/User/pages/Setting/index'
        }, {
            iconUrl: app.globalData.STATIC_PREFIX + '/static/images/icon-personal-help.png',
            name: '使用帮助',
            id: 'instructions',
            path: '/Extra/pages/Help/index'
        }]
        wx.getSystemInfo({
            success(res) {
                that.setData({
                    safeArea: {
                        ...res.safeArea,
                        top: res.safeArea.top + wx.getMenuButtonBoundingClientRect().height,
                    },
                    haveWatch: wx.getStorageSync('deviceList').some((item) => {
                        return item.device_type == 0 || item.device_type == '0'
                    })
                }, () => {
                    if (!that.data.haveWatch) {
                        that.setData({
                            menus: menus.filter((item) => item.name != '健康分析')
                        })
                    } else {
                        that.setData({
                            menus
                        })
                    }
                })
            }
        })

        const res = await fetchElderlyInfo({
            id: wx.getStorageSync('elderlyInfo').id
        });
        if (res.return_code == 'SUCCESS') {
            this.setData({
                userInfo: res.data
            })
        } else {
            wx.showToast({
                title: res.return_msg,
                icon: 'none'
            });
        }
    },
    go(e) {
        const {
            currentTarget: {
                dataset: {
                    id,
                    path
                }
            }
        } = e
        customNavigate({
            url: path
        })
    },
    async doLogout() {
        showModal('您确定要退出登录吗？', async () => {
            const {
                id
            } = wx.getStorageSync('userInfo');
            const res = await fetchLogout({
                id
            });
            if (res.return_code == 'SUCCESS') {
                wx.clearStorage();
                showToast(res.return_msg, () => {
                    setTimeout(() => {
                        customRedirect({
                            url: '/User/pages/Login/index'
                        });
                    }, 500);
                })
            } else {
                showToast(res.return_msg)
            }
        })
    }
})